package com.yiren.backstage.currency.bean;

public class PointExBean extends PointBean {

    /*** 相对原点的角度 */
    public float angle;
    /*** 相对原点的距离 */
    public double length;

    public float getAngle() {
        return angle;
    }

    public void setAngle(float angle) {
        this.angle = angle;
    }

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public PointExBean() {

    }

   /* public PointEx(float x, float y) {
        setPoint(x, y);
    }

    public PointEx(float angle, double length) {
        setPoint(angle, length);
    }*/

    @Override
    public void setPoint(float x, float y) {
        super.setPoint(x, y);
        fixAngleLength();
    }

    @Override
    public void setPoint(PointBean other) {
        super.setPoint(other);
        fixAngleLength();
    }

    public void setPoint(float angle, double length) {
        this.angle = angle;
        this.length = length;
        fixXY();
    }

    public void setPoint(PointExBean other) {
        this.x = other.getX();
        this.y = other.getY();
        this.angle = other.angle;
        this.length = other.length;
    }

    public void addXY(float x, float y) {
        this.x += x;
        this.y += y;
        this.x = Math.round(this.x * 1000) / 1000.0f;
        this.y = Math.round(this.y * 1000) / 1000.0f;
    }

    public void addXY(PointBean other) {
        addXY(other.getX(), other.getY());
    }

    public void fixXY() {
        double a = angle * Math.PI / 180;
        x = Math.round(Math.sin(a) * length * 1000) / 1000.0f;
        y = Math.round(Math.cos(a) * length * 1000) / 1000.0f;
    }

    public void fixAngleLength() {
        length = Math.sqrt(x * x + y * y);
        if (length == 0) {
            angle = 0;
        } else {
            double a = Math.asin(x / length);
            if (x >= 0 && y >= 0) {
                angle = (float) (a * 180 / Math.PI);
            } else if (x >= 0 && y < 0) {
                angle = 180 - (float) (a * 180 / Math.PI);
            } else if (x < 0 && y < 0) {
                angle = 180 - (float) (a * 180 / Math.PI);
            } else if (x < 0 && y >= 0) {
                angle = 360 + (float) (a * 180 / Math.PI);
            }
        }
    }
}
